var tableId;
var articleIds = [];
var pageListUrl = '/Olala/article/gArticlePageList';
layui.use('table', function () {
    var table = layui.table;

    tableId = table.render({
        elem: '#article_list'
        , height: 600
        , url: pageListUrl //数据接口
        , page: true //开启分页
        , toolbar: '#article_list_tool'
        , method: 'post'
        , response: {
            statusCode: 20 //规定成功的状态码，默认：0
        }
        , cols: [
            [ //表头
                {field: "checkbox", title: "选择", type: "checkbox", width: "5%"},
                {field: 'articleId', title: 'ID', width: "10%", sort: true}
                ,
                {
                    field: 'articleTitle', title: '标题', width: "30%",
                    templet: function (data) {
                        return "<a href='javascript:void(0)' onclick='article_update(" + data.articleId + ")'>" + data.articleTitle + "</a>"
                    }
                },
                {
                    field: 'articleType', title: '分类', width: "10%", sort: true,
                    templet: function (data) {
                        return data.articleType === 0 ? "特惠" : data.articleType === 1 ? "公告" : "其他";
                    }
                }
                , {field: 'source', title: '资源', width: "10%"}
                , {field: 'updateTime', title: '更新时间', width: "15%", sort: true}
                , {field: 'clickCount', title: '浏览量', width: "10%", sort: true}
                ,
                {
                    field: 'isEnabled', title: '是否启用', width: "5%", align: "center",
                    templet: function (data) {
                        return data.isEnabled ? "是" : "否";
                    }
                }
                ,
                {
                    title: "操作", width: "5%",
                    templet: function (data) {
                        return " <button class=\"layui-btn layui-btn-sm\" lay-event=\"add\" onclick='article_publish(" + data.articleId + ")'>发布</button>"
                    }
                }
            ]
        ]
    });

    table.on('checkbox(article_list_filter)', function (obj) {
        if (obj.checked) {
            articleIds[obj.data.articleId] = obj.data.articleId;
        } else {
            articleIds.splice(obj.data.articleId, 1);
        }
    });

    //监听事件
    table.on('toolbar(article_list_filter)', function (obj) {
        switch (obj.event) {
            case 'add':
                article_add();
                break;
            case 'delete':
                article_del();
                break;
        }
    });

});

function article_add() {
    var index = layer.open({
        type: 2,
        title: 'layer mobile页',
        shade: 0.8,
        content: '/Olala/server/article-add.html',
        end: function () {
            tableId.reload();
        }
    });
    layer.full(index);
}

function article_del() {
    var index = layer.confirm('确认删除？', {
        btn: ['确认', '取消'], //按钮
        end: function () {
            tableId.reload();
        }
    }, function () {
        var ids = [];
        for (var i = 0; i < articleIds.length; i++) {
            if (articleIds[i]) {
                ids.push(articleIds[i]);
            }
        }
        console.log(ids)
        $.ajax({
            url: "/Olala/article/delByIds",
            data: {
                ids: ids
            },
            traditional: true,
            dataType: "json",
            type: "post",
            success: function (result) {
                layer.msg(result.msg);
                layer.close(index);
            }
        });
    }, function () {
        layer.close(index);
    });
}

function article_update(id) {
    localStorage.setItem("article_update_frame_articleId", id);
    var index = layer.open({
        type: 2,
        title: '资讯',
        shade: 0.8,
        content: '/Olala/server/article-update.html',
        end: function () {
            tableId.reload();
        }
    });
    layer.full(index);
}

function article_publish(id) {
    var index = layer.confirm('发布状态？', {
        btn: ['发布', '下架', '取消'], //按钮
        end: function () {
            tableId.reload();
        }
    }, function () {
        $.post("/Olala/article/updateIsEnableById", {articleId: id, isEnable: true}, function (result) {
            layer.msg(result.msg);
        }, "json");
        layer.close(index);
    }, function () {
        $.post("/Olala/article/updateIsEnableById", {articleId: id, isEnable: false}, function (result) {
            layer.msg(result.msg);
        }, "json");
        layer.close(index);
    }, function () {
        layer.close(index);
    });
}
